<template>
  <div class="cararea-container">
    <div class="create-container">
      <el-button type="primary" @click="openCard"
        >添加区域</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" width="180">
        <template slot-scope="scope">
         {{ params.pageSize*(params.page-1)+scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="areaName" label="区域名称" width="180">
      </el-table-column>
      <el-table-column prop="spaceNumber" label="车位数（个）">
      </el-table-column>
      <el-table-column prop="areaProportion" label="面积(m²)">
      </el-table-column>
      <el-table-column prop="ruleName" label="计费规则">
         </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" type="text" @click="dialog(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="delRent(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="pageChange"
      :current-page="params.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.pageSize"
      layout="total,  prev, pager, next, sizes, jumper"
      :total="total"
    >
    </el-pagination>
     <!-- chuangkou -->
  <el-dialog
  title="编辑楼宇"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">

  <el-form :model="form" :rules="rules" ref="form">
    <el-form-item label="区域名称" prop="areaName">
      <el-input v-model="form.areaName" autocomplete="off" placeholder="请输入区域名称"></el-input>
    </el-form-item>
    <el-form-item label="车位数（个）" prop="spaceNumber">
      <el-input v-model="form.spaceNumber" autocomplete="off" placeholder="请输入车位个数"></el-input>
    </el-form-item>
    <el-form-item class="one" label="面积（m²）" prop="areaProportion">
      <div class="oneSpan">
        <el-input v-model="form.areaProportion" autocomplete="off" placeholder="请输入面积"></el-input>
      <div class="span">m²</div>
      </div>
    </el-form-item>
    <el-form-item class="two" label="关联计费规则" prop="ruleId">
      <el-select v-model="form.ruleId" placeholder="请选择租户">
            <el-option
              v-for="item in ruleList"
              :key="item.id"
              :value="item.id"
              :label="item.ruleNameView"
            />
          </el-select>
        <!-- <el-input v-model="form.ruleId" autocomplete="off" placeholder="请选择"></el-input> -->
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="form.remark" autocomplete="off" placeholder="请输入备注"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
import { getCarAreaList,addCarAreaAPI,getRuleListAPI,updateCarAreaAPI,delRuleAPI } from "@/api/carArea";
import { Message } from 'element-ui';
export default {
  data() {
    return {
      getId:'',
      ruleList:[],
      form:{
        areaName:'',
        spaceNumber:'',
        areaProportion:'',
        ruleId:'',
        remark:''
      },
      rules:{
        areaName:[
          { required: true, message: "请输入楼宇名称", trigger: "blur" },
        ],
        spaceNumber:[
          { required: true, message: "请输入楼宇层数", trigger: "blur" },
        ],
        areaProportion:[
          { required: true, message: "请输入在管面积", trigger: "blur" },
        ],
        ruleId:[
          { required: true, message: "请输入物业费", trigger: "blur" },
        ]
      },
      dialogVisible:false,
      tableData: [],
      total: 0,
      params: {
        page: "1",
        pageSize: "10",
      },
    };
  },
   async created() {
    this.render(this.params);
    let {data} = await getRuleListAPI();
    this.ruleList=data.rows;
  },
  methods: {
    openCard(){
      this.getId=''
      this.dialogVisible = true
    },
    dialog(list){
      const{areaName,spaceNumber,areaProportion,ruleId,remark}=list
      this.form={areaName,spaceNumber,areaProportion,ruleId,remark}
      this.dialogVisible = true
      this.getId=list.id
    },
    async delRent(id){
      console.log(id);
      
      await this.$confirm('确定删除吗?')
      try{
        await delRuleAPI(id)
      Message.success('删除成功！');
      this.render(this.params)
      }catch(err){
        Message.error(err);
      }
    },
    confirmSubmit(){
this.$refs.form.validate(async (valid) => {
        if(!valid)return;
        if(!this.getId){
try{
          await addCarAreaAPI(this.form)
          Message.success('添加成功！');
          this.dialogVisible = false
        this.render(this.params);
        }catch(err){
          Message.error(err);
        }
        }else{
          try{
            await updateCarAreaAPI({
              ...this.form,
              id: this.getId
            })
            Message.success('修改成功！');
            this.dialogVisible = false
        this.render(this.params);
          }catch(err){
            Message.error(err);
          }
        }
        
      });
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.render(this.params);
    },
    pageChange(page) {
      this.params.page = page;
      this.render(this.params);
    },
    async render(params) {
      try {
        const { data } = await getCarAreaList(params);
        this.total = data.total;
        this.tableData = data.rows;
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
.one .span{
  text-align: center;
  width: 50px;
  background-color: rgba(0,0,0,0.05);
}
.oneSpan{
  width: 100%;
  display: flex;
}
::v-deep .el-button.el-button--primary{
  margin-bottom: 20px;
}
::v-deep .el-table__header-wrapper th{
  background-color:#f4f6f8
}
::v-deep .el-pagination{
  margin-top: 30px;
  text-align: right;
}

</style>

